package cn.edu.jju.psytest.persist.util;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import cn.edu.jju.psytest.model.vo.TestResultVo;

public class QuizRecordUtil {

	private static DecimalFormat df = new DecimalFormat("0.00");
	
	public static int[] qth = {1,4,12,27,40,42,48,49,52,53,56,58};
	public static int[] qpzz = {3,9,10,28,38,45,46,51,55,65};
	public static int[] rjgxmg = {6,21,34,36,37,41,61,69,73};
	public static int[] yy = {5,14,15,20,2,26,29,30,31,32,54,71,79};
	public static int[] jl = {2,17,23,33,39,57,72,78,80,86};
	public static int[] dd = {11,24,63,67,74,81};
	public static int[] kb = {13,25,47,50,70,75,82};
	public static int[] pz = {8,18,43,68,76,83};
	public static int[] jsbx = {7,16,35,62,77,84,85,87,88,90};
	public static int[] qt = {19,44,59,60,64,66,89};
	
	public static TestResultVo getTestResult(String answerS) {
		String[] answers = answerS.split(":");
		
		int ts = 0;
		String tas = "";
		int nopi = 0;
		int noni = 0;
		String pi = "";
		String ni = "";
		Map<String, String> fas = new HashMap<String, String>();
		
		for(int i=0; i < answers.length; i++) {
			int answer = Integer.parseInt(answers[i]);
			ts += answer;
			if(answer > 1) {
				nopi += 1;
				pi += i + 1 + ",";
			} else {
				noni += 1;
				ni += i + 1 + ",";
			}
		}
		pi = pi.substring(0, pi.lastIndexOf(","));
		ni = ni.substring(0, ni.lastIndexOf(","));
		tas = formatFloat((float)ts / 90);
		
		/**
		 * qth
		 */
		int qthScore = 0;
		for(int i=0; i < qth.length; i++) {
			int qno = qth[i];
			qthScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("qth", formatFloat((float)qthScore / qth.length));
		
		/**
		 * qpzz
		 */
		int qpzzScore = 0;
		for(int i=0; i < qpzz.length; i++) {
			int qno = qpzz[i];
			qpzzScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("qpzz", formatFloat((float)qpzzScore / qpzz.length));
		
		/**
		 * rjgxmg
		 */
		int rjgxmgScore = 0;
		for(int i=0; i < rjgxmg.length; i++) {
			int qno = rjgxmg[i];
			rjgxmgScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("rjgxmg", formatFloat((float)rjgxmgScore / rjgxmg.length));
		
		/**
		 * yy
		 */
		int yyScore = 0;
		for(int i=0; i < yy.length; i++) {
			int qno = yy[i];
			yyScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("yy", formatFloat((float)yyScore / yy.length));
		
		/**
		 * jl
		 */
		int jlScore = 0;
		for(int i=0; i < jl.length; i++) {
			int qno = jl[i];
			jlScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("jl", formatFloat((float)jlScore / jl.length));
		
		/**
		 * dd
		 */
		int ddScore = 0;
		for(int i=0; i < dd.length; i++) {
			int qno = dd[i];
			ddScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("dd", formatFloat((float)ddScore / dd.length));
		
		/**
		 * kb
		 */
		int kbScore = 0;
		for(int i=0; i < kb.length; i++) {
			int qno = kb[i];
			kbScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("kb", formatFloat((float)kbScore / kb.length));
		
		/**
		 * pz 
		 */
		int pzScore = 0;
		for(int i=0; i < pz.length; i++) {
			int qno = pz[i];
			pzScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("pz", formatFloat((float)pzScore / pz.length));
		
		/**
		 * jsbx 
		 */
		int jsbxScore = 0;
		for(int i=0; i < jsbx.length; i++) {
			int qno = jsbx[i];
			jsbxScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("jsbx", formatFloat((float)jsbxScore / jsbx.length));
		
		/**
		 * qt 
		 */
		int qtScore = 0;
		for(int i=0; i < qt.length; i++) {
			int qno = qt[i];
			qtScore += Integer.parseInt(answers[qno - 1]);
		}
		fas.put("qt", formatFloat((float)qtScore / qt.length));
		
		TestResultVo trVo = new TestResultVo();
		trVo.setTotalScore(ts + "");
		trVo.setTotalAverageScore(tas);
		trVo.setNumOfPositiveItems(nopi + "");
		trVo.setPositiveItems(pi);
		trVo.setNumOfNegativeItems(noni + "");
		trVo.setNegativeItems(ni);
		trVo.setFactorsAverageScore(fas);
		return trVo;
	}
	
	private static String formatFloat(float f) {
		return df.format(f);
	}
}
